package com.qf.deviceprovider8002.controller;

import com.qf.java2106.pojo.Device;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author q
 * @version V1.0
 * @date 2022/01/17
 **/
@RestController
@RequestMapping("/pro/device")
public class DevProController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/hello")
    public String hello(){
        return "DevPro -> " + port;
    }

    @GetMapping("/get/resttemplate")
    public String getRestTemplate(Integer id, String name){
        return "get getRestTemplate" + id + " " + name + " " + port;
    }

    @PostMapping("/post/resttemplate")
    public String postRestTemplate(Integer id, String name, @CookieValue(required = false, name = "token") String token){
        return "post postRestTemplate" + id + " " + name + " " + port + "---token:" + token;
    }

    //=================fegin参数传递开始==========
    //普通参数
    @GetMapping("/testVar")
    public String testVar(Integer id){
        if (id == 1){
            System.out.println("id=1");
            id = 10/0;
        } else if (id == 2){
            System.out.println("id=2");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "testVar " + port + ":" + id;
    }

    //路径传参
    @GetMapping("/testUrlVar/{name}")
    public String testUrlVar(@PathVariable String name){
        return "testVar " + port + ":" + name;
    }

    //多个参数
    @GetMapping("/testManyVar")
    public String testManyVar(Integer id, String name){
        return "testManyVar " + port + ":" + id + " " + name;
    }

    //实体参数
    @PostMapping("/testJson")
    public Device testJson(@RequestBody Device device){
        return device;
    }

    @PostMapping("/testCookie")
    public String testCookie(@RequestBody Device device, @CookieValue(required = false, name = "token") String token){
        return device + "-->" + token;
    }


    //=================fegin参数传递结束==========

}
